package  
{
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.net.SharedObject;
    import flash.net.SharedObjectFlushStatus;
	import flash.external.ExternalInterface
	import flash.display.*;
	import flash.net.URLLoader;
	import flash.system.*;
	import com.adobe.serialization.json.*;
	import fl.events.*;
	import fl.controls.Button;
	import fl.controls.TextInput;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;

	
	/**
	 * ...
	 * @author egoing
	 */
	public class Profile extends MovieClip
	{
		private var _service:String;
		private var _id:String;
		private var _feedback:Boolean;
		private var logo:Loader;
		public  var loginBtn:Button;
		public  var closeBtn:Button;
		public  var id:TextInput;
		public  var password:TextInput;
		public 	var description:TextField; 
		public 	var idLabel:TextField; 
		public 	var passLabel:TextField; 
		public var bg:Shape;
		
		
		public function Profile() 
		{
			Security.allowDomain('*');
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
			
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.align = 'lt';
			var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
			_service = paramObj.service;
			_id = paramObj.id == undefined ? 'egoing' : paramObj.id;
			
			if (ExternalInterface.available) {
				try{
					ExternalInterface.addCallback('login', loginService);
				} catch (e:Error) {
					trace(e);
				}
			}
			
			stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
			_service = 'twitter';
			
			logo = new Loader();
			logo.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event) {
				logo.x = 124;
				logo.y = 15;
				var ratio = logo.height / 32;
				logo.height = 32;
				logo.width = logo.width / ratio;
				repaint();
			} );
			try{
				logo.load(new URLRequest('http://monkeyfly.egoing.net/images/twitpic.png'));
			} catch (e:Error) {
			
			}
			id = new TextInput();
			password = new TextInput();
			loginBtn = new Button();
			closeBtn = new Button();
			description = new TextField();
			bg = new Shape();
			
			idLabel.text = 'id';
			idLabel.selectable = false;
			passLabel.text = 'password';
			passLabel.selectable = false;
			password.displayAsPassword = true;
			loginBtn.label = 'login';
			closeBtn.label = 'close';
			var tf:TextFormat = new TextFormat('Verdana', 11);
			description.defaultTextFormat = tf;
			description.multiline = true;
			description.wordWrap = true;
			addChild(bg);
			addChild(logo);
			addChild(id);
			addChild(password);
			addChild(loginBtn);
			addChild(closeBtn);
			addChild(description);
			addChild(idLabel);
			addChild(passLabel);
			setChildIndex(idLabel, getChildIndex(logo));
			setChildIndex(passLabel, getChildIndex(logo));
			
			repaint();
			
			loginBtn.addEventListener(MouseEvent.CLICK, buttonHandler);
			closeBtn.addEventListener(MouseEvent.CLICK, buttonHandler);
			password.addEventListener(ComponentEvent.ENTER, buttonHandler);
			stage.addEventListener(Event.RESIZE, function(e:Event) { 
				repaint()
			} );
		}
		
		private function repaint() {
			var _x = 123;
			var yspacing = 10;
			id.x = _x;
			id.y = logo.y + logo.height+yspacing;
			id.width = 250;
			password.x = _x;
			password.y = id.y + id.height+yspacing;
			password.width = 250;
			loginBtn.x = _x;
			loginBtn.height = 32;
			loginBtn.width = 73;
			loginBtn.y = password.y + password.height+yspacing;
			closeBtn.x = loginBtn.x+loginBtn.width+10;
			closeBtn.y = loginBtn.y;
			closeBtn.height = 32;
			closeBtn.width = 73;
			description.x = _x;
			description.y = loginBtn.y + loginBtn.height + yspacing;
			description.width = id.width;
			description.height = stage.stageHeight - description.y - 10
			idLabel.x = 15;
			idLabel.y = id.y
			passLabel.x = idLabel.x
			passLabel.y = password.y;
			
			bg.graphics.clear();
			bg.graphics.beginFill(0xFFFFFF);
			bg.graphics.drawRoundRect(0, 0, stage.stageWidth, stage.stageHeight, 9);
			bg.graphics.endFill();
			
		}
		
		private function buttonHandler(e:Event):void {
			switch(e.target) {
				case loginBtn:
				case password:
					if (ExternalInterface.available) {
						ExternalInterface.call('$.cookie',this._id+'_'+this._service+'_auth', JSON.encode({id:id.text, password:password.text}));
						if(_feedback)
							ExternalInterface.call('onLoginComplete',{target:this._service});
					}
					break;
				case closeBtn:
						ExternalInterface.call('popup','profile', 'hide');
					break;
			}
		}
		
		private function loginService(_service:String,_desc:String,_feedback:Boolean) {
			this._service = _service;
			description.htmlText = _desc;
			this._feedback = _feedback;
		}

	}
	
}